package org.javaforever.gatescore.vue;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.complexverb.Assign;
import org.javaforever.gatescore.complexverb.ListMyActive;
import org.javaforever.gatescore.complexverb.ListMyAvailableActive;
import org.javaforever.gatescore.complexverb.Revoke;
import org.javaforever.gatescore.core.FrontManyToMany;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.UtilsMethodGenerator;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.utils.StringUtil;
import org.javaforever.gatescore.verb.ListActive;

public class ElementUIMtmPage extends ElementUIPage{
	private static final long serialVersionUID = 551411229367131296L;
	protected FrontManyToMany mtm;
	
	public ElementUIMtmPage (FrontManyToMany mtm) {
		super();
		this.mtm = mtm;
		this.standardName = mtm.getStandardName();
	}

	public FrontManyToMany getMtm() {
		return mtm;
	}

	public void setMtm(FrontManyToMany mtm) {
		this.mtm = mtm;
		this.standardName = mtm.getStandardName();
	}

	@Override
	public StatementList generateStatementList() throws Exception{
		if (this.mtm.getSlave().isLegacy()||this.mtm.getMaster().isLegacy()) return null;
		Assign assign = new Assign(this.mtm);
		Revoke revoke = new Revoke(this.mtm);
		ListMyActive lma = new ListMyActive(this.mtm);
		ListMyAvailableActive lmaa = new ListMyAvailableActive(this.mtm);
		ListActive mla = new ListActive(this.mtm.getMaster());
		ListActive sla = new ListActive(this.mtm.getSlave());		
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"<style>"));
		sList.add(new Statement(2000L,1,".el-transfer-panel{"));
		sList.add(new Statement(3000L,1,"height:700px;"));
		sList.add(new Statement(4000L,1,"}"));
		sList.add(new Statement(5000L,1,".el-transfer-panel__list{"));
		sList.add(new Statement(6000L,1,"height: 650px;"));
		sList.add(new Statement(7000L,1,"}"));
		sList.add(new Statement(7100L,1,".el-card {"));
		sList.add(new Statement(7200L,1,"font-size:12pt;"));
		sList.add(new Statement(7300L,1,"line-height:20px;"));
		sList.add(new Statement(7400L,1,"}"));
		sList.add(new Statement(8000L,0,"</style>"));
		sList.add(new Statement(9000L,0,"<template>"));
		sList.add(new Statement(10000L,1,"<div class=\"app-container\">"));
		sList.add(new Statement(11000L,2,"<el-row>"));
		sList.add(new Statement(12000L,3,"<el-col :span=\"4\">"));
		sList.add(new Statement(13000L,4,"<el-card :height=\"700\" class=\"box-card\" >"));
		sList.add(new Statement(14000L,5,"<div slot=\"header\" class=\"clearfix\" style=\"margin-top:-5px; margin-bottom:-20px\">"));
		if (this.mtm.getMaster().getLanguage().equalsIgnoreCase("english")) {
			sList.add(new Statement(15000L,6,"<span>Active "+this.mtm.getMaster().getText()+"</span>"));
		}else {
			sList.add(new Statement(15000L,6,"<span>活跃"+this.mtm.getMaster().getText()+"</span>"));
		}
		sList.add(new Statement(16000L,5,"</div>"));
		sList.add(new Statement(17000L,5,"<el-radio-group v-model=\""+this.mtm.getMaster().getLowerFirstDomainName()+"Id\" style=\"margin-top:-18px\">"));
		sList.add(new Statement(18000L,6,"<div v-for=\""+this.mtm.getMaster().getLowerFirstDomainName()+" in this.active"+this.mtm.getMaster().getCapFirstPlural()+"\" :key=\""+this.mtm.getMaster().getLowerFirstDomainName()+"."+this.mtm.getMaster().getDomainId().getLowerFirstFieldName()+"\" class=\"text item\">"));
		sList.add(new Statement(19000L,7,"<el-button type=\"text\"><el-radio :label=\""+this.mtm.getMaster().getLowerFirstDomainName()+"."+this.mtm.getMaster().getDomainId().getLowerFirstFieldName()+"\" name=\""+this.mtm.getMaster().getLowerFirstDomainName()+"Id\" @change=\"listAvailableActive"+this.mtm.getMaster().getCapFirstDomainName()+""+this.mtm.getSlave().getAliasOrNamePlural()+"Using"+this.mtm.getMaster().getCapFirstDomainName()+"Id("+this.mtm.getMaster().getLowerFirstDomainName()+"."+this.mtm.getMaster().getDomainId().getLowerFirstFieldName()+")\">{{ "+this.mtm.getMaster().getLowerFirstDomainName()+"."+this.mtm.getMaster().getDomainName().getLowerFirstFieldName()+" }}</el-radio></el-button>"));
		sList.add(new Statement(20000L,6,"</div>"));
		sList.add(new Statement(21000L,5,"</el-radio-group>"));
		sList.add(new Statement(22000L,4,"</el-card>"));
		sList.add(new Statement(23000L,3,"</el-col>"));
		sList.add(new Statement(24000L,3,"<el-col :span=\"12\">"));
		sList.add(new Statement(25000L,4,"<el-transfer v-model=\"this.availableActive"+this.mtm.getSlave().getAliasOrNamePlural()+"\" :data=\"this.activeAll"+this.mtm.getSlave().getAliasOrNamePlural()+"\" :titles=\"this.transHeaders\" :props=\"{key: '"+this.mtm.getSlave().getDomainId().getLowerFirstFieldName()+"', label: '"+StringUtil.lowerFirst(this.mtm.getSlave().getDomainName().getFieldName())+"'}\" @change=\"handleChange\"/>"));
		sList.add(new Statement(26000L,3,"</el-col>"));
		sList.add(new Statement(27000L,2,"</el-row>"));
		sList.add(new Statement(28000L,1,"</div>"));
		sList.add(new Statement(29000L,0,"</template>"));
		sList.add(new Statement(30000L,0,"<script>"));
		sList.add(new Statement(31000L,0,"import {"));
		sList.add(new Statement(32000L,1,mla.generateApiMethod().getLowerFirstMethodName() + ", "+lma.generateApiMethod().getLowerFirstMethodName()+", "+lmaa.generateApiMethod().getLowerFirstMethodName()+", "+assign.generateApiMethod().getLowerFirstMethodName()+", "+revoke.generateApiMethod().getLowerFirstMethodName()));
		sList.add(new Statement(33000L,0,"} from '@/api/"+this.mtm.getMaster().getPlural().toLowerCase()+"'"));
		sList.add(new Statement(34000L,0,""));
		sList.add(new Statement(35000L,0,"import {"));
		sList.add(new Statement(36000L,1,sla.generateApiMethod().getLowerFirstMethodName()));
		sList.add(new Statement(37000L,0,"} from '@/api/"+this.mtm.getSlave().getPlural().toLowerCase()+"'"));
		sList.add(new Statement(38000L,0,""));
		sList.add(new Statement(39000L,0,"export function isBlank(val) {"));
		sList.add(new Statement(40000L,1,"if (val === undefined || val === null || val === '' || val === '0' || val === 0) return true"));
		sList.add(new Statement(41000L,1,"else return false"));
		sList.add(new Statement(42000L,0,"}"));
		sList.add(new Statement(43000L,0,""));
		sList.add(new Statement(44000L,0,"export default {"));
		sList.add(new Statement(45000L,1,"inject: ['reload'],"));
		sList.add(new Statement(46000L,1,"name: 'link"+this.mtm.getMaster().getCapFirstDomainName()+""+StringUtil.capFirst(this.mtm.getSlave().getAliasOrName())+"',"));
		sList.add(new Statement(47000L,1,"filters: {"));
		sList.add(new Statement(48000L,1,"},"));
		sList.add(new Statement(49000L,1,"data() {"));
		sList.add(new Statement(50000L,2,"return {"));
		sList.add(new Statement(51000L,3,""+this.mtm.getMaster().getLowerFirstDomainName()+"Id: 1,"));
		sList.add(new Statement(52000L,3,"active"+this.mtm.getMaster().getCapFirstPlural()+": [],"));
		sList.add(new Statement(53000L,3,"activeAll"+this.mtm.getSlave().getAliasOrNamePlural()+": [],"));
		sList.add(new Statement(54000L,3,"availableActive"+this.mtm.getSlave().getAliasOrNamePlural()+": [],"));
		if (this.mtm.getMaster().getLanguage().equalsIgnoreCase("english")) {
			sList.add(new Statement(55000L,3,"transHeaders: ['Current "+this.mtm.getSlave().getText()+"', 'Available "+this.mtm.getSlave().getText()+"']"));
		}else {
			sList.add(new Statement(55000L,3,"transHeaders: ['已有"+this.mtm.getSlave().getText()+"', '可添加"+this.mtm.getSlave().getText()+"']"));
		}
		sList.add(new Statement(56000L,2,"}"));
		sList.add(new Statement(57000L,1,"},"));
		sList.add(new Statement(58000L,1,"created() {"));
		sList.add(new Statement(59000L,2,"this.listActive"+this.mtm.getMaster().getCapFirstPlural()+"()"));
		sList.add(new Statement(60000L,2,"this.listAllActive"+this.mtm.getSlave().getAliasOrNamePlural()+"()"));
		sList.add(new Statement(61000L,2,"this.listAvailableActive"+this.mtm.getMaster().getCapFirstDomainName()+""+this.mtm.getSlave().getAliasOrNamePlural()+"Using"+this.mtm.getMaster().getCapFirstDomainName()+"Id(this."+this.mtm.getMaster().getLowerFirstDomainName()+"Id)"));
		sList.add(new Statement(62000L,1,"},"));
		sList.add(new Statement(63000L,1,"methods: {"));
		StatementList sl = mla.generateControllerMethod().getMethodStatementList();
		sl.setSerial(64000L);
		sList.add(sl);		

		sList.add(new Statement(71000L,2,"listAllActive"+this.mtm.getSlave().getAliasOrNamePlural()+"() {"));
		sList.add(new Statement(72000L,3,"listActive"+this.mtm.getSlave().getCapFirstPlural()+"().then("));
		sList.add(new Statement(73000L,4,"response => {"));
		sList.add(new Statement(74000L,5,"this.activeAll"+this.mtm.getSlave().getAliasOrNamePlural()+" = response.data.rows"));
		sList.add(new Statement(75000L,4,"})"));
		sList.add(new Statement(76000L,2,"},"));
		
		sl = lma.generateControllerMethod().getMethodStatementList();
		sl.setSerial(77000L);
		sList.add(sl);
		
		sl = lmaa.generateControllerMethod().getMethodStatementList();
		sl.setSerial(78000L);
		sList.add(sl);	
		
		sl = assign.generateControllerMethod().getMethodStatementList();
		sl.setSerial(79000L);
		sList.add(sl);	
		
		sl = revoke.generateControllerMethod().getMethodStatementList();
		sl.setSerial(80000L);
		sList.add(sl);	

		sList.add(new Statement(99000L,2,"handleChange(value, direction, movedKeys) {"));
		sList.add(new Statement(100000L,3,"console.log(value, direction, movedKeys)"));
		sList.add(new Statement(101000L,3,"// 可以通过direction回调right/left 来进行操作，right：把数字移到右边，left把数据移到左边"));
		sList.add(new Statement(102000L,3,"if (direction === 'right') {"));
		sList.add(new Statement(103000L,4,"this.revoke"+this.mtm.getSlave().getAliasOrNamePlural()+"From"+this.mtm.getMaster().getCapFirstDomainName()+"(this."+this.mtm.getMaster().getLowerFirstDomainName()+"Id, movedKeys.join(','))"));
		sList.add(new Statement(104000L,3,"}"));
		sList.add(new Statement(105000L,3,"if (direction === 'left') {"));
		sList.add(new Statement(106000L,4,"this.assign"+this.mtm.getSlave().getAliasOrNamePlural()+"To"+this.mtm.getMaster().getCapFirstDomainName()+"(this."+this.mtm.getMaster().getLowerFirstDomainName()+"Id, movedKeys.join(','))"));
		sList.add(new Statement(107000L,3,"}"));
		sList.add(new Statement(108000L,2,"},"));
		
		sl = UtilsMethodGenerator.generateSleepMethodStatements();
		sl.setSerial(109000L);
		sList.add(sl);	

		sList.add(new Statement(115000L,1,"}"));
		sList.add(new Statement(116000L,0,"}"));
		sList.add(new Statement(117000L,0,"</script>"));
		return WriteableUtil.merge(sList);
	}
}
